package org.example.env_boot.config;

import org.example.env_boot.entity.AQI;
import org.example.env_boot.entity.Admin;
import org.example.env_boot.entity.GridCity;
import org.example.env_boot.entity.GridProvince;
import org.example.env_boot.repository.AQIRepository;
import org.example.env_boot.repository.AdminRepository;
import org.example.env_boot.repository.GridCityRepository;
import org.example.env_boot.repository.GridProvinceRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * 数据初始化组件
 */
@Component
public class DataInitializer implements CommandLineRunner {
    
    private final AQIRepository aqiRepository;
    private final GridProvinceRepository provinceRepository;
    private final GridCityRepository cityRepository;
    private final AdminRepository adminRepository;
    
    public DataInitializer(AQIRepository aqiRepository, GridProvinceRepository provinceRepository,
                          GridCityRepository cityRepository, AdminRepository adminRepository) {
        this.aqiRepository = aqiRepository;
        this.provinceRepository = provinceRepository;
        this.cityRepository = cityRepository;
        this.adminRepository = adminRepository;
    }
    
    @Override
    public void run(String... args) throws Exception {
        initAQIData();
        initProvinceData();
        initCityData();
        initAdminData();
    }
    
    private void initAQIData() {
        if (aqiRepository.count() == 0) {
            // 创建6个AQI级别 - 使用setter方法
            
            // 1. 优
            AQI aqi1 = new AQI();
            aqi1.setChineseExplain("优");
            aqi1.setAqiExplain("Good");
            aqi1.setColor("#00E400");
            aqi1.setHealthImpact("空气质量令人满意，基本无空气污染");
            aqi1.setTakeSteps("各类人群可正常活动");
            aqi1.setSo2Min(0);
            aqi1.setSo2Max(50);
            aqi1.setCoMin(0);
            aqi1.setCoMax(2);
            aqi1.setSpmMin(0);
            aqi1.setSpmMax(35);
            aqiRepository.save(aqi1);
            
            // 2. 良
            AQI aqi2 = new AQI();
            aqi2.setChineseExplain("良");
            aqi2.setAqiExplain("Moderate");
            aqi2.setColor("#FFFF00");
            aqi2.setHealthImpact("空气质量可接受，但某些污染物可能对极少数异常敏感人群健康有较弱影响");
            aqi2.setTakeSteps("极少数异常敏感人群应减少户外活动");
            aqi2.setSo2Min(51);
            aqi2.setSo2Max(150);
            aqi2.setCoMin(3);
            aqi2.setCoMax(9);
            aqi2.setSpmMin(36);
            aqi2.setSpmMax(75);
            aqiRepository.save(aqi2);
            
            // 3. 轻度污染
            AQI aqi3 = new AQI();
            aqi3.setChineseExplain("轻度污染");
            aqi3.setAqiExplain("Unhealthy for Sensitive Groups");
            aqi3.setColor("#FF7E00");
            aqi3.setHealthImpact("易感人群症状有轻度加剧，健康人群出现刺激症状");
            aqi3.setTakeSteps("儿童、老年人及心脏病、呼吸系统疾病患者应减少长时间、高强度的户外锻炼");
            aqi3.setSo2Min(151);
            aqi3.setSo2Max(475);
            aqi3.setCoMin(10);
            aqi3.setCoMax(35);
            aqi3.setSpmMin(76);
            aqi3.setSpmMax(115);
            aqiRepository.save(aqi3);
            
            // 4. 中度污染
            AQI aqi4 = new AQI();
            aqi4.setChineseExplain("中度污染");
            aqi4.setAqiExplain("Unhealthy");
            aqi4.setColor("#FF0000");
            aqi4.setHealthImpact("进一步加剧易感人群症状，可能对健康人群心脏、呼吸系统有影响");
            aqi4.setTakeSteps("儿童、老年人及心脏病、呼吸系统疾病患者避免长时间、高强度的户外锻炼，一般人群适量减少户外运动");
            aqi4.setSo2Min(476);
            aqi4.setSo2Max(800);
            aqi4.setCoMin(36);
            aqi4.setCoMax(60);
            aqi4.setSpmMin(116);
            aqi4.setSpmMax(150);
            aqiRepository.save(aqi4);
            
            // 5. 重度污染
            AQI aqi5 = new AQI();
            aqi5.setChineseExplain("重度污染");
            aqi5.setAqiExplain("Very Unhealthy");
            aqi5.setColor("#99004C");
            aqi5.setHealthImpact("心脏病和肺病患者症状显著加剧，运动耐受力降低，健康人群普遍出现症状");
            aqi5.setTakeSteps("儿童、老年人及心脏病、肺病患者应停留在室内，停止户外运动，一般人群减少户外运动");
            aqi5.setSo2Min(801);
            aqi5.setSo2Max(1600);
            aqi5.setCoMin(61);
            aqi5.setCoMax(90);
            aqi5.setSpmMin(151);
            aqi5.setSpmMax(250);
            aqiRepository.save(aqi5);
            
            // 6. 严重污染
            AQI aqi6 = new AQI();
            aqi6.setChineseExplain("严重污染");
            aqi6.setAqiExplain("Hazardous");
            aqi6.setColor("#7E0023");
            aqi6.setHealthImpact("健康人群运动耐受力降低，有明显强烈症状，提前出现某些疾病");
            aqi6.setTakeSteps("儿童、老年人和病人应停留在室内，避免体力消耗，一般人群避免户外活动");
            aqi6.setSo2Min(1601);
            aqi6.setSo2Max(2100);
            aqi6.setCoMin(91);
            aqi6.setCoMax(120);
            aqi6.setSpmMin(251);
            aqi6.setSpmMax(350);
            aqiRepository.save(aqi6);
            
            System.out.println("AQI级别数据初始化完成");
        }
    }
    
    private void initProvinceData() {
        if (provinceRepository.count() == 0) {
            String[][] provinces = {
                {"北京市", "京"}, {"天津市", "津"}, {"河北省", "冀"}, {"山西省", "晋"},
                {"内蒙古自治区", "蒙"}, {"辽宁省", "辽"}, {"吉林省", "吉"}, {"黑龙江省", "黑"},
                {"上海市", "沪"}, {"江苏省", "苏"}, {"浙江省", "浙"}, {"安徽省", "皖"},
                {"福建省", "闽"}, {"江西省", "赣"}, {"山东省", "鲁"}, {"河南省", "豫"},
                {"湖北省", "鄂"}, {"湖南省", "湘"}, {"广东省", "粤"}, {"广西壮族自治区", "桂"},
                {"海南省", "琼"}, {"重庆市", "渝"}, {"四川省", "川"}, {"贵州省", "黔"},
                {"云南省", "滇"}, {"西藏自治区", "藏"}, {"陕西省", "陕"}, {"甘肃省", "甘"},
                {"青海省", "青"}, {"宁夏回族自治区", "宁"}, {"新疆维吾尔自治区", "新"}
            };
            
            for (String[] province : provinces) {
                GridProvince gridProvince = new GridProvince();
                gridProvince.setProvinceName(province[0]);
                gridProvince.setProvinceAbbr(province[1]);
                gridProvince.setRemarks("系统初始化数据");
                provinceRepository.save(gridProvince);
            }
            System.out.println("省份数据初始化完成");
        }
    }
    
    private void initCityData() {
        if (cityRepository.count() == 0) {
            // 为前几个省份添加一些主要城市
            GridProvince beijing = provinceRepository.findByProvinceName("北京市").orElse(null);
            GridProvince shanghai = provinceRepository.findByProvinceName("上海市").orElse(null);
            GridProvince guangdong = provinceRepository.findByProvinceName("广东省").orElse(null);
            GridProvince jiangsu = provinceRepository.findByProvinceName("江苏省").orElse(null);
            
            if (beijing != null) {
                String[] beijingCities = {"东城区", "西城区", "朝阳区", "丰台区", "石景山区", "海淀区", "顺义区", "通州区", "大兴区", "房山区", "门头沟区", "昌平区", "平谷区", "密云区", "怀柔区", "延庆区"};
                for (String cityName : beijingCities) {
                    GridCity city = new GridCity();
                    city.setCityName(cityName);
                    city.setProvinceId(beijing.getProvinceId());
                    city.setRemarks("系统初始化数据");
                    cityRepository.save(city);
                }
            }
            
            if (shanghai != null) {
                String[] shanghaiCities = {"黄浦区", "徐汇区", "长宁区", "静安区", "普陀区", "虹口区", "杨浦区", "闵行区", "宝山区", "嘉定区", "浦东新区", "金山区", "松江区", "青浦区", "奉贤区", "崇明区"};
                for (String cityName : shanghaiCities) {
                    GridCity city = new GridCity();
                    city.setCityName(cityName);
                    city.setProvinceId(shanghai.getProvinceId());
                    city.setRemarks("系统初始化数据");
                    cityRepository.save(city);
                }
            }
            
            if (guangdong != null) {
                String[] guangdongCities = {"广州市", "深圳市", "珠海市", "汕头市", "佛山市", "韶关市", "湛江市", "肇庆市", "江门市", "茂名市", "惠州市", "梅州市", "汕尾市", "河源市", "阳江市", "清远市", "东莞市", "中山市", "潮州市", "揭阳市", "云浮市"};
                for (String cityName : guangdongCities) {
                    GridCity city = new GridCity();
                    city.setCityName(cityName);
                    city.setProvinceId(guangdong.getProvinceId());
                    city.setRemarks("系统初始化数据");
                    cityRepository.save(city);
                }
            }
            
            if (jiangsu != null) {
                String[] jiangsuCities = {"南京市", "无锡市", "徐州市", "常州市", "苏州市", "南通市", "连云港市", "淮安市", "盐城市", "扬州市", "镇江市", "泰州市", "宿迁市"};
                for (String cityName : jiangsuCities) {
                    GridCity city = new GridCity();
                    city.setCityName(cityName);
                    city.setProvinceId(jiangsu.getProvinceId());
                    city.setRemarks("系统初始化数据");
                    cityRepository.save(city);
                }
            }
            
            System.out.println("城市数据初始化完成");
        }
    }
    
    private void initAdminData() {
        if (adminRepository.count() == 0) {
            // 创建默认管理员账号
            Admin admin = new Admin();
            admin.setAdminCode("admin");
            // 直接使用明文密码，不进行加密
            admin.setPassword("123456");
            admin.setRemarks("系统默认管理员账号");
            adminRepository.save(admin);
            
            System.out.println("管理员数据初始化完成 - 账号: admin, 密码: 123456");
        }
    }
} 